package hu.myai.indicators.tick;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TickVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Is for the % of size change of a tick compare to the previous.
 *
 * @author bozbalint
 *
 * */
public class TickSizeChangeIndicator extends AbstractIndicator {

    Double prevValue = null;

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    TickVO tickPrev;

    /**
     * Constructor
     */
    public TickSizeChangeIndicator() {
        super(null);
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        // indicator init
        if (tickPrev == null) {
            tickPrev = lastTick;
            return null;
        }

        // average size of a tick
        double sizePrev = (tickPrev.getHigh() - tickPrev.getLow() + Math.abs(tickPrev.getOpen() - tickPrev.getClose())) / 2;
        double sizeLast = (lastTick.getHigh() - lastTick.getLow() + Math.abs(lastTick.getOpen() - lastTick.getClose())) / 2;

        IndicatorVO indicator = new IndicatorVO();

        // the % of the size change
        indicator.setValue(sizeLast / sizePrev);
        indicator.setDate(lastTick.getTime());

        if (prevValue == null) {
            indicator.setChanged(true);
        } else {
            indicator.setChanged(prevValue != indicator.getValue());
        }

        prevValue = indicator.getValue();
        tickPrev = lastTick;

        log.debug(indicator.toString());
        return indicator;
    }
}
